package com.xinweisi.dataserver.module.memberdata.memberbehavior;

import com.xinweisi.dataserver.common.domain.PageResultDTO;
import com.xinweisi.dataserver.common.domain.ResponseDTO;
import com.xinweisi.dataserver.module.memberdata.memberbehavior.domain.dto.MemberBehaviorQueryDTO;
import com.xinweisi.dataserver.module.memberdata.memberbehavior.domain.vo.MemberBehaviorVO;
import com.xinweisi.dataserver.util.DBNameUtil;
import com.xinweisi.dataserver.util.DateUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/api/memberBehavior")
public class MemberBehaviorController {
    @Autowired
    MemberBehaviorService memberBehaviorService;
    @Value("${startDBName}")
    private String OldDBName;

    @ApiOperation(value = "分页查会员行为", notes = "分页查会员行为")
    @RequestMapping(value = "/getMemberBehavior",method= RequestMethod.POST)
    public ResponseDTO<PageResultDTO<MemberBehaviorVO>> getMemberBehavior(@RequestBody @Valid MemberBehaviorQueryDTO queryDTO) {
        log.info("----------进入查询会员行为----------");
        String startTime;
        if(queryDTO.getStartTime()!=null && !"".equals(queryDTO.getStartTime())){
            startTime = queryDTO.getStartTime();
        }else {
            startTime = DateUtil.getDateAll(DateUtil.getToDayInitial());
            queryDTO.setStartTime(startTime);
            queryDTO.setEndTime(DateUtil.getDateAll(DateUtil.getToDayInitialEnd()));
        }
        String endTime;
        if(queryDTO.getEndTime()!=null && !"".equals(queryDTO.getEndTime())){
            endTime = queryDTO.getEndTime();
        }else {
            endTime =queryDTO.getStartTime();
        }
        startTime = startTime.substring(0,startTime.indexOf(" "));
        endTime = endTime.substring(0,endTime.indexOf(" "));
        queryDTO.setDBNameList(DBNameUtil.getDANameList(OldDBName,startTime,endTime));

        return memberBehaviorService.getMemberBehavior(queryDTO);
    }
}
